﻿Imports QLTHUVIEN.DTO
Imports QLTHUVIEN.BUS
Public Class frmDocGia
    Private Sub layDanhSach()
        Dim tdgiaBus As New TheDocGiaBUS()
        Dim dt As DataTable = tdgiaBus.LayDanhSach()
        dtgDanhSachDocGia.DataSource = dt
    End Sub

    Private Sub layLoaiDocGia()
        Dim ldgia As LoaiDocGiaBUS = New LoaiDocGiaBUS()
        Dim dt As DataTable = ldgia.LayDanhSach()
        cboMaLoaiDocGia.DataSource = dt
        cboMaLoaiDocGia.ValueMember = "MaLoaiDocGia"
        cboMaLoaiDocGia.DisplayMember = "TenLoaiDocGia"
    End Sub

    Private Sub layNguoiTiepNhan()
        Dim nvBus As NhanVienBUS = New NhanVienBUS()
        Dim dt As DataTable = nvBus.layNhanVienBoPhan("Thủ Thư")
        cboMaNguoiLapThe.DataSource = dt
        cboMaNguoiLapThe.ValueMember = "MaNhanVien"
        cboMaNguoiLapThe.DisplayMember = "TenNhanVien"
    End Sub

    Private Sub frmDocGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        layDanhSach()
        layLoaiDocGia()
        layNguoiTiepNhan()
    End Sub

    Private Function layDocGia(ByRef docGia As TheDocGiaDTO) As Boolean
        Try
            docGia.TenDocGia = txtTenDocGia.Text.ToString()
            docGia.DiaChiDocGia = txtDiaChi.Text.ToString()
            docGia.NgaySinh = Date.Parse(dtpNgaySinh.Text)
            docGia.Email = txtEmail.Text.ToString()
            docGia.NgayLapThe = Date.Parse(dtpNgayLapThe.Text)
            docGia.TienNo = Double.Parse(txtTienNo.Text)
            docGia.MaLoaiDocGia = Integer.Parse(cboMaLoaiDocGia.SelectedValue.ToString())
            docGia.MaNguoiLapThe = Integer.Parse(cboMaNguoiLapThe.SelectedValue.ToString())
            docGia.IsDelete = False
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnThemDocGia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemDocGia.Click
        Dim docGia As TheDocGiaDTO = New TheDocGiaDTO()
        If layDocGia(docGia) = False Then
            MessageBox.Show("kt lai Du Lieu Nhap ")
            Return
        End If

        Dim dgiaBus As New TheDocGiaBUS()
        dgiaBus.ThemDocGia(docGia)

        layDanhSach()
    End Sub

    Private Sub btnSuaDocGia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaDocGia.Click
        Dim docGia As TheDocGiaDTO = New TheDocGiaDTO()
        If txtMaDocGia.Text = "" Then
            Return
        End If

        docGia.MaDocGia = Integer.Parse(txtMaDocGia.Text)
        If layDocGia(docGia) = False Then
            MessageBox.Show("kt lai Du Lieu Nhap ")
            Return
        End If

        Dim dgiaBus As New TheDocGiaBUS()
        dgiaBus.SuaTheDocGia(docGia)

        layDanhSach()
    End Sub

    Private Sub ganGiaTri()
        txtMaDocGia.Text = dtgDanhSachDocGia.CurrentRow.Cells(0).Value.ToString()
        txtTenDocGia.Text = dtgDanhSachDocGia.CurrentRow.Cells(1).Value.ToString()
        txtDiaChi.Text = dtgDanhSachDocGia.CurrentRow.Cells(2).Value.ToString()
        dtpNgaySinh.Text = dtgDanhSachDocGia.CurrentRow.Cells(3).Value.ToString()
        txtEmail.Text = dtgDanhSachDocGia.CurrentRow.Cells(4).Value.ToString()
        dtpNgayLapThe.Text = dtgDanhSachDocGia.CurrentRow.Cells(5).Value.ToString()
        txtTienNo.Text = dtgDanhSachDocGia.CurrentRow.Cells(6).Value.ToString()
        cboMaLoaiDocGia.Text = dtgDanhSachDocGia.CurrentRow.Cells(7).Value.ToString()
        cboMaNguoiLapThe.Text = dtgDanhSachDocGia.CurrentRow.Cells(8).Value.ToString()
    End Sub
    Private Sub dtgDanhSachDocGia_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgDanhSachDocGia.CellClick
        ganGiaTri()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim docGia As TheDocGiaDTO = New TheDocGiaDTO()
        If txtMaDocGia.Text = "" Then
            Return
        End If

        docGia.MaDocGia = Integer.Parse(txtMaDocGia.Text)
        docGia.IsDelete = True

        Dim dgiaBus As TheDocGiaBUS = New TheDocGiaBUS()
        dgiaBus.XoaTheDocGia(docGia)

        layDanhSach()
    End Sub
End Class